package com.lzy.webapi.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.SysUserAdminServiceApi;
import com.lzy.base.apis.SysUserRoleServiceApi;
import com.lzy.base.dtos.SysUserAdminDto;
import com.lzy.base.dtos.SysUserRoleDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.product.apis.*;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.*;
import com.lzy.webapi.response.ProductContentResponse;
import com.lzy.webapi.response.ProductImageTextResponse;
import com.lzy.webapi.response.ProductInfoResponse;
import com.lzy.webapi.response.ProductPromotionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by WIN7 on 2017/12/6.
 */
@Component
public class ProductFacade {
    @Autowired
    ProductInfoServiceApi productInfoServiceApi;

    @Autowired
    ProductImageTextServiceApi productImageTextServiceApi;

    @Autowired
    ProductInfoProjectServiceApi productInfoProjectServiceApi;

    @Autowired
    ProjectServiceApi projectServiceApi;

    @Autowired
    SysUserAdminServiceApi sysUserAdminServiceApi;

    @Autowired
    ProductClassifyDeptServiceApi classifyDeptServiceApi;

    @Autowired
    ProductClassifyServiceApi classifyServiceApi;

    @Autowired
    SysUserRoleServiceApi userRoleServiceApi;

    /**
     * 获取产品详情
     */
    @DataSource(name = DataSource.DATASOURCE_PRODUCT)
    public ProductInfoResponse getProductDetail(Long productId) {
        ProductInfoResponse productInfoResponse = new ProductInfoResponse();
        //获取产品基础表的信息
        ProductInfoDto productInfoDto = productInfoServiceApi.findById(productId);
        if (productInfoDto != null) {
            productInfoResponse.setProductId(String.valueOf(productInfoDto.getId()));
            productInfoResponse.setProductName(productInfoDto.getName());
            productInfoResponse.setProductImageUrl(productInfoDto.getCover());
            productInfoResponse.setPrice(productInfoDto.getPrice());
            if (productInfoDto.getVideo() != null) {
                productInfoResponse.setVideoUrl(productInfoDto.getVideo());
            } else {
                productInfoResponse.setVideoUrl(productInfoDto.getCover());
            }
            productInfoResponse.setOffice(productInfoDto.getOffice());
            productInfoResponse.setOfficeName(productInfoDto.getOfficeName());
            productInfoResponse.setRemark(productInfoDto.getRemark());

            //获取图文详情
            List<ProductImageTextResponse> imageTextResponseList = new ArrayList<>();
            ProductImageTextDto imageTextDto = new ProductImageTextDto();
            imageTextDto.setProductId(productId);
            List<ProductImageTextDto> imageTextDtoList = productImageTextServiceApi.query(imageTextDto);
            if (imageTextDtoList != null && imageTextDtoList.size() > 0) {
                for (ProductImageTextDto pitDto : imageTextDtoList) {
                    ProductImageTextResponse productImageTextResponse = new ProductImageTextResponse();
                    productImageTextResponse.setContent(pitDto.getPath());
                    productImageTextResponse.setId(pitDto.getId());
                    productImageTextResponse.setProductId(pitDto.getProductId());
                    productImageTextResponse.setRemark(pitDto.getRemark());
                    productImageTextResponse.setSort(pitDto.getSort());
                    productImageTextResponse.setType(pitDto.getType());

                    imageTextResponseList.add(productImageTextResponse);
                }
                productInfoResponse.setDetails(imageTextResponseList);
            }

            //获取产品内容(即固定项)
            List<ProductContentResponse> productContentResponseList = new ArrayList<>();
            ProductInfoProjectDto proInfoProjectDto = new ProductInfoProjectDto();
            proInfoProjectDto.setProductId(productId);
            proInfoProjectDto.setType(BusinessType.ProType.FIXED.getCode());
            List<ProductInfoProjectDto> infoProjectDtoList = productInfoProjectServiceApi.query(proInfoProjectDto);
            if (infoProjectDtoList != null && infoProjectDtoList.size() > 0) {
                for (ProductInfoProjectDto pipDto : infoProjectDtoList) {
                    if (pipDto != null && pipDto.getProjectId() != null) {
                        ProjectDto projectDto = projectServiceApi.getById(pipDto.getProjectId());
                        if (projectDto != null) {
                            ProductContentResponse contentResponse = new ProductContentResponse();
                            contentResponse.setPromotionId(String.valueOf(projectDto.getId()));
                            contentResponse.setPromotionName(projectDto.getName());
                            contentResponse.setPromotionUnit(projectDto.getUnit());
                            contentResponse.setPromotionValue(String.valueOf(pipDto.getContainNum()));
                            productContentResponseList.add(contentResponse);
                        }
                    }
                }
                productInfoResponse.setPackages(productContentResponseList);
            }

            //获取产品内容(即单选项)
            List<ProductPromotionResponse> productPromotionResponseList = new ArrayList<>();
            ProductInfoProjectDto proInfoProjectDto1 = new ProductInfoProjectDto();
            proInfoProjectDto1.setProductId(productId);
            proInfoProjectDto1.setType(BusinessType.ProType.SINGLE.getCode());
            List<ProductInfoProjectDto> infoProjectDtoList1 = productInfoProjectServiceApi.query(proInfoProjectDto1);
            if (infoProjectDtoList1 != null && infoProjectDtoList1.size() > 0) {
                for (ProductInfoProjectDto pipDto : infoProjectDtoList1) {
                    if (pipDto != null && pipDto.getProjectId() != null) {
                        ProjectDto projectDto = projectServiceApi.getById(pipDto.getProjectId());
                        if (projectDto != null) {
                            ProductPromotionResponse promotionResponse = new ProductPromotionResponse();
                            promotionResponse.setPromotionId(String.valueOf(projectDto.getId()));
                            promotionResponse.setPromotionName(projectDto.getName());
                            promotionResponse.setPromotionUnit(projectDto.getUnit());
                            promotionResponse.setPromotionValue(String.valueOf(pipDto.getContainNum()));
                            productPromotionResponseList.add(promotionResponse);
                        }
                    }
                }
                productInfoResponse.setPromotions(productPromotionResponseList);
            }
            return productInfoResponse;
        }
        return null;
    }

    /**
     * 获取业务员的产品列表
     */
    public PageInfo<ProductInfoDto> getProductList(Pager pager, Long userId) {
        //首先确定该用户的身份，是业务员还是管理员
        SysUserRoleDto userRoleDto = new SysUserRoleDto();
        userRoleDto.setUserId(userId);
        SysUserRoleDto surDto = userRoleServiceApi.selectByUserRole(userRoleDto);
        if (surDto != null) {
            //业务员
            if (surDto.getRoleId() == 3) {
                //获取该业务员的部门id
                SysUserAdminDto userAdminDto = sysUserAdminServiceApi.selectByPrimaryKey(userId);
                if (userAdminDto != null && userAdminDto.getDeptId() != null) {
                    //根据部门id获取所有关联产品分类的集合
                    ProductClassifyDeptDto classifyDeptDto = new ProductClassifyDeptDto();
                    classifyDeptDto.setDeptId(userAdminDto.getDeptId());
                    List<ProductClassifyDeptDto> classifyDeptDtoList = classifyDeptServiceApi.selectProductClassifyDept(classifyDeptDto);
                    //存放正常产品分类的集合
                    List<Long> idList = new ArrayList<>();
                    //存放要下架的产品分类的集合
                    List<Long> idListDown = new ArrayList<>();
                    if (classifyDeptDtoList != null) {
                        for (ProductClassifyDeptDto pcdDto : classifyDeptDtoList) {
                            if (pcdDto != null && pcdDto.getClassifyId() != null) {
                                //根据分类id获取产品分类的剩余量和销售时间
                                ProductClassifyDto pcDto = classifyServiceApi.findById(pcdDto.getClassifyId());
                                if (pcDto != null) {
                                    //产品分类销售时间存在
                                    if (!pcDto.getStartTime().equals("") && !pcDto.getEndTime().equals("")) {
                                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
                                        String dateString = formatter.format(new Date());
                                        Integer m = classifyServiceApi.compareDate(String.valueOf(pcDto.getStartTime()), dateString);
                                        Integer n = classifyServiceApi.compareDate(String.valueOf(pcDto.getEndTime()), dateString);
                                        //m == 1表示未开始，n == 3表示已结束，其它情况表示进行中
                                        if (m != 1 && n != 3) {
                                            //存在库存
                                            if (pcDto.getSurplus() != null && pcDto.getSurplus() > 0) {
                                                idList.add(pcDto.getId());
                                            } else if (pcDto.getSurplus() == null) {
                                                //不存在库存
                                                idList.add(pcDto.getId());
                                            } else if (pcDto.getSurplus() == 0) {
                                                //库存里的产品已售完，需要下架该分类
                                                idListDown.add(pcDto.getId());
                                            }
                                        }
                                        //不存在销售时间
                                    } else if (pcDto.getStartTime().equals("") && pcDto.getEndTime().equals("")) {
                                        //存在库存
                                        if (pcDto.getSurplus() != null && pcDto.getSurplus() > 0) {
                                            idList.add(pcDto.getId());
                                        } else if (pcDto.getSurplus() == 0) {
                                            //库存里的产品已售完，需要下架该分类
                                            idListDown.add(pcDto.getId());
                                        }
                                    }
                                }
                            }
                        }
                        //批量更新下架产品分类
                        String idDownList = "";
                        if (idListDown != null && idListDown.size() > 0) {
                            for (long l : idListDown) {
                                idDownList = idDownList + l + ",";
                            }
                            if (!idDownList.equals("")) {
                                //更新产品分类下架
                                classifyServiceApi.updateShelves(BusinessType.ShelfEnum.PULL.getK(), idDownList.substring(0, idDownList.length() - 1));
                            }
                        }

                        //根据产品分类id查询产品列表
                        String ids = "";
                        if (idList != null && idList.size() > 0) {
                            for (long l : idList) {
                                ids = ids + l + ",";
                            }
                            if (!ids.equals("")) {
                                ProductInfoDto productInfoDto1 = new ProductInfoDto();
                                productInfoDto1.setStatus(BusinessType.StatusEnum.ENABLED.getCode());
                                productInfoDto1.setShelves(BusinessType.ShelfEnum.PUT.getK());
                                productInfoDto1.setVerifyStatus(BusinessType.VerifyStatusEnum.YESVERIFY.getK());
                                productInfoDto1.setType(BusinessType.ProductType.MEAL.getCode());
                                PageInfo<ProductInfoDto> pageInfo3 = productInfoServiceApi.selectListByClassifyIdList(pager, productInfoDto1, ids.substring(0, ids.length() - 1));
                                if (pageInfo3 != null) {
                                    return pageInfo3;
                                }
                            }
                        }
                    }
                }
            } else if (surDto.getRoleId() == 1) {
                //管理员
                ProductInfoDto piDto = new ProductInfoDto();
                piDto.setStatus(BusinessType.StatusEnum.ENABLED.getCode());
                piDto.setShelves(BusinessType.ShelfEnum.PUT.getK());
                piDto.setVerifyStatus(BusinessType.VerifyStatusEnum.YESVERIFY.getK());
                piDto.setType(BusinessType.ProductType.MEAL.getCode());
                PageInfo<ProductInfoDto> pageInfo2 = productInfoServiceApi.selectList(pager, piDto);
                if (pageInfo2 != null) {
                    return pageInfo2;
                }
            }
        }
        return null;
    }

    //根据产品分类id查询产品列表
//    public PageInfo<ProductClassifyDto> getPageInfo(Pager pager,List<Long> idList){
//        String ids = "";
//        if (idList != null && idList.size() > 0) {
//            for (long l : idList) {
//                ids = ids + l + ",";
//            }
//            if (!ids.equals("")) {
//                ProductInfoDto productInfoDto = new ProductInfoDto();
//                productInfoDto.setStatus(BusinessType.StatusEnum.ENABLED.getCode());
//                productInfoDto.setShelves(BusinessType.ShelfEnum.PUT.getK());
//                productInfoDto.setVerifyStatus(BusinessType.VerifyStatusEnum.YESVERIFY.getK());
//                productInfoDto.setType(BusinessType.ProductType.MEAL.getCode());
//                PageInfo<ProductInfoDto> pageInfo = productInfoServiceApi.selectListByClassifyIdList(pager,productInfoDto, ids.substring(0, ids.length() - 1));
//                if (pageInfo != null) {
//                    return pageInfo;
//                }
//            }
//        }
//        return null;
//    }
}
